Envoy - 初探

##基础知识

网络代理/网络服务器

Envoy基本概念:集群,监听器,路由和过滤器

首先,有一个监听器。这告诉Envoy绑定到一个端口,在本例中为10000:

接下来,监听器有一个过滤器。过滤器告诉监听器如何处理它收到的请求,并为Envoy提供一系列过滤器。

如果您需要做一些复杂的事情,那么要对每个请求使用几个过滤器。

envoy.http_connection_manager 过滤器,它用于代理HTTP请求。

HTTP连接管理器还有一个适用的HTTP过滤器列表(请参阅HTTP过滤器列表)。

其中最重要的是将envoy.router 的请求路由到后端的过滤器。

TCP过滤器列表: https://www.envoyproxy.io/docs/envoy/v1.8.0/api-v2/api/v2/listener/listener.proto#listener-filter

HTTP过滤器列表: https://www.envoyproxy.io/docs/envoy/v1.8.0/api-v2/config/filter/network/http_connection_manager/v2/http_connection_manager.proto#envoy-api-msg-config-filter-network-http-connection-manager-v2-httpfilter


static_resources:
listeners:

  • name: listener_0
    address:
    socket_address: { address: 0.0.0.0, port_value: 10000 }
    filter_chains:
    • filters:
      • name: envoy.http_connection_manager
        config:
        access_log:
        • name: envoy.file_access_log
          config:
          path: “/var/log/envoy.log”
          codec_type: auto
          stat_prefix: ingress_http
          route_config:
          name: local_route
          virtual_hosts:
          • name: local_service
            domains: [“*”]
            routes:
            • match: { prefix: “/datacenter.AdverseEvent” }
              route:
              cluster: dc_ae
              max_grpc_timeout: 0s
            • match: { prefix: “/izhaohu-adverse/api/adverse” }
              route:
              cluster: dc_ae
              max_grpc_timeout: 0s
              cors:
              allow_origin:
              • “*”
                allow_methods: GET, PUT, DELETE, POST, OPTIONS
                allow_headers: keep-alive,user-agent,cache-control,content-type,content-transfer-encoding,custom-header-1,x-accept-content-transfer-encoding,x-accept-response-streaming,x-user-agent,x-grpc-web,grpc-timeout
                max_age: “1728000”
                expose_headers: custom-header-1,grpc-status,grpc-message
                enabled: true
                http_filters:
                //一个翻译器 他帮助grpc 与 json互通。
        • name: envoy.grpc_json_transcoder
          config:
          //翻译器的字典,需要通过proto生成
          proto_descriptor: “/data/descriptor.pb”
          services: [“datacenter.AdverseEvent”]
          print_options:
          add_whitespace: false
          always_print_primitive_fields: true
          always_print_enums_as_ints: false
          preserve_proto_field_names: true
          
        • name: envoy.grpc_web
        • name: envoy.cors
        • name: envoy.router
          //服务模块
          clusters:
  • name: dc_ae
    connect_timeout: 1.25s
    type: logical_dns
    http2_protocol_options: {}
    lb_policy: round_robin
    hosts: [{ socket_address: { address: dc-ae, port_value: 60001 }}]

1
2
3
4
5
6
7
8
9
生成descriptor.pb
protoc -I$GOPATH/src \
-I$GOPATH/src/github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis \
-I$GOPATH/src/protobuf/src/ \
-I$GOPATH/src/datacenter/proto \
--include_imports \
--include_source_info \
--descriptor_set_out=../build/envoy/descriptor.pb \
*.proto

请参阅 案例分享